﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;
using System.Xml.Linq;
using A2DFramework.AssemblyService;
using A2DFramework.CommandBusService.MSMQCommandBus.Attributes;

namespace A2DFramework.CommandBusService.MSMQCommandBus.Configuration
{
    public static class MSMQCommandBusConfiguration
    {
        public static string CommandQueueNameFormat;
        public static string CommandResponseQueueConnectionString;
        public static string NetworkLocation;
        public static bool AutoCreateIfNotExists;
        public static bool WebSessionSupport;
        public static bool AutoDeleteNetworkFile;

        public static void Load()
        {
            //load setings from xml file
            XElement root = XElement.Load(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "A2D.config"));

            root = root.Element("CommandBusSetting");
            if (root == null)
                throw new Exception("CommandBusSetting node not exists");

            CommandQueueNameFormat = root.Element("MSMQCommandBus").Element("CommandQueue").Value;
            CommandResponseQueueConnectionString = root.Element("MSMQCommandBus").Element("ResponseQueue").Value;
            AutoCreateIfNotExists = bool.Parse(root.Element("MSMQCommandBus").Element("AutoCreateIfNotExists").Value);
            NetworkLocation = root.Element("MSMQCommandBus").Element("NetworkLocation").Value;
            if (root.Element("MSMQCommandBus").Element("WebSessionSupport") != null
                && root.Element("MSMQCommandBus").Element("WebSessionSupport").Value != null
                && root.Element("MSMQCommandBus").Element("WebSessionSupport").Value.Trim().Length > 0)
                WebSessionSupport = bool.Parse(root.Element("MSMQCommandBus").Element("WebSessionSupport").Value);
            else
                WebSessionSupport = true;//default support session transfer

            if (root.Element("MSMQCommandBus").Element("AutoDeleteNetworkFile") != null
                && root.Element("MSMQCommandBus").Element("AutoDeleteNetworkFile").Value != null
                && root.Element("MSMQCommandBus").Element("AutoDeleteNetworkFile").Value.Trim().Length > 0)
                AutoDeleteNetworkFile = bool.Parse(root.Element("MSMQCommandBus").Element("AutoDeleteNetworkFile").Value);
            else
                AutoDeleteNetworkFile = true;//default auto delete
            

            //prepare msmq
            PrepareResponseMSMQ();
        }
        public static void PrepareResponseMSMQ()
        {
            if (!MessageQueue.Exists(CommandResponseQueueConnectionString))
                if (AutoCreateIfNotExists)
                    MessageQueue.Create(CommandResponseQueueConnectionString, true);
        }
        public static void PrepareCommandMSMQ(Type cmdType)
        {
            string fullQueueName = GetQueueName(cmdType);
            if (!MessageQueue.Exists(fullQueueName))
                if (AutoCreateIfNotExists)
                    MessageQueue.Create(fullQueueName, true);
        }

        private static Dictionary<Type, string> queueNames = new Dictionary<Type, string>();
        public static string GetQueueName(Type cmdType)
        {
            if(queueNames.ContainsKey(cmdType))
                return queueNames[cmdType];

            string queueName;
            QueueNameAttribute attr = AttributeUtils.GetAttribute<QueueNameAttribute>(cmdType);
            if (attr == null)
                queueName = cmdType.Name;
            else
                queueName = attr.QueueIdentity;
            string fullQueueName = string.Format(CommandQueueNameFormat, queueName);

            queueNames[cmdType] = fullQueueName;

            return fullQueueName;
        }
    }
}
